/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.admin.addins;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryResult;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.gidoo.cms.admin.AdminPage;
import org.gidoo.cms.login.GidooCMSSession;

/**
 * A panel where you can adminstrate the sections.
 * @author <a href="mailto:thomas@berlin-reloaded.de">Thomas Krause</a>
 */
public class SectionsPanel extends AddInPanel
{

  private Form formNewSection;
  private TextField<String> txtURL;
  private TextField<String> txtName;
  private Button btAddSection;
  private AdminPage theParent;

  /** Creates a new instance of UsersPanel */
  public SectionsPanel(String id, AdminPage parent)
  {

    super(id);
    this.theParent = parent;

    formNewSection = new Form("formNewSection");
    add(formNewSection);

    txtURL = new TextField<String>("txtURL", new Model(""));
    txtName = new TextField<String>("txtName", new Model(""));
    txtURL.setRequired(false);
    txtName.setRequired(true);

    formNewSection.add(txtURL);
    formNewSection.add(txtName);

    btAddSection = new Button("btAddSection", new ResourceModel("addsection"))
    {
      @Override
      public void onSubmit()
      {
        addNewSection();
      }
    };

    formNewSection.add(btAddSection);
  }

  private void addNewSection()
  {

    try
    {
      String url = txtURL.getModelObject();

      if(url == null)
      {
        url = "";
      }

      String name = txtName.getModelObject();

      GidooCMSSession session = (GidooCMSSession) getSession();

      Node sectionsNode = session.getJcrRootNode().getNode("sections");
      QueryResult r = session.getQueryManager().createQuery(
        "/jcr:root/sections/*[@cms:url = '" + url + "']", Query.XPATH).execute();

      // test for existance first
      if(sectionsNode.hasNode(name) || r.getNodes().hasNext())
      {
        error(getString("sectionexists"));
        return;
      }

      Node section = sectionsNode.addNode(name, "cms:section");

      section.setProperty("cms:url", url);
      sectionsNode.save();

      theParent.updateDataAndView();
      info(getString("sectioncreated"));
    }
    catch(RepositoryException ex)
    {
      Logger.getLogger(SectionsPanel.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);

      error(getString("jcrerror"));
    }

  }
}

